//
//  IMAudioMessageTableViewCell.m
//  MLIM
//
//  Created by molon on 15/6/28.
//  Copyright (c) 2015年 molon. All rights reserved.
//

#import "IMAudioMessageTableViewCell.h"
#import <MLCommonAudioPlayButton.h>
#import "IMCommon.h"
#import "IMMessageBackgroundImageView.h"
#import "IMUserAvatarImageView.h"

#define MSG ((IMAudioMessage*)self.imMessage)
@interface IMAudioMessageTableViewCell()

@property (nonatomic, strong) MLCommonAudioPlayButton *playButton;
@property (nonatomic, strong) UILabel *durantionLabel;
@property (nonatomic, strong) UIImageView *unreadSignImageView;

@end

@implementation IMAudioMessageTableViewCell

- (void)resetWithIsInit:(BOOL)isInit
{
    [super resetWithIsInit:isInit];
    
    if (isInit) {
        [self.backgroundImageView addSubview:self.playButton];
        [self.contentView addSubview:self.durantionLabel];
        [self.contentView addSubview:self.unreadSignImageView];
        
        WEAKSELF
        [self.playButton setDurationChangedBlock:^(double duration, MLAudioPlayButton *button) {
            STRONGSELF
            ((IMAudioMessage*)sSelf.imMessage).duration = duration;
            [sSelf updateDurationAndUnhearedSign];
        }];
        [self.playButton setAudioStateChangedBlock:^(MLAudioPlayButtonState audioState, MLAudioPlayButton *button) {
            STRONGSELF
            [sSelf updateDurationAndUnhearedSign];
        }];
        [self.playButton setPreferredWidthChangedBlock:^(CGFloat preffredWidth, MLAudioPlayButton *button) {
            STRONGSELF
            [sSelf setNeedsLayout];
        }];
        [self.playButton setAudioWillPlayBlock:^(MLAudioPlayButton *button) {
            STRONGSELF
            sSelf.unreadSignImageView.hidden = YES;
            [sSelf changeBackgroundImageToDark:YES];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [sSelf changeBackgroundImageToDark:NO];
            });
        }];
        [self.playButton setDidReceivePlayErrorBlock:^(NSError *error, MLAudioPlayButton *button) {
            SHOW_SIMPLE_TIPS(@"播放音频失败");
        }];
    }
}

- (void)updateDurationAndUnhearedSign
{
    if (self.playButton.duration>0&&self.playButton.audioState==MLAudioPlayButtonStateNormal) {
        self.durantionLabel.text = [NSString stringWithFormat:@"%.f''",floor(self.playButton.duration+0.5f)];
        self.unreadSignImageView.hidden = IMISME(self.imMessage.clientId)?YES:(!MSG.unheared);
    }else{
        self.durantionLabel.text = @"";
        self.unreadSignImageView.hidden = YES;
    }
}

#pragma mark - getter
- (MLCommonAudioPlayButton *)playButton
{
    if (!_playButton) {
        _playButton = [MLCommonAudioPlayButton new];
        _playButton.customBundleName = IMCONFIGM.audioPlayImageBundleName;
        _playButton.dontAutoSetDuration = YES;
    }
    return _playButton;
}

- (UILabel *)durantionLabel
{
    if (!_durantionLabel) {
        UILabel* label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor colorWithWhite:0.614 alpha:1.000];
        label.font = [UIFont boldSystemFontOfSize:13.0f];
        label.textAlignment = NSTextAlignmentRight;
        
        _durantionLabel = label;
    }
    return _durantionLabel;
}

- (UIImageView *)unreadSignImageView
{
    if (!_unreadSignImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = IMIMG(@"音频未读标记");
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        
        _unreadSignImageView = imageView;
    }
    return _unreadSignImageView;
}

#pragma mark - setter
- (void)setImMessage:(IMMessage *)imMessage
{
    NSAssert(!imMessage||[imMessage isKindOfClass:[IMAudioMessage class]], @"IMAudioMessageCell只接受IMAudioMessage对象");
    [super setImMessage:imMessage];
    
    if (MSG.duration>0) {
        self.playButton.dontAutoSetDuration = YES;
        self.playButton.duration = MSG.duration;
    }else{
        self.playButton.dontAutoSetDuration = NO;
    }
    
    [self.playButton setAudioWithURL:MSG.audioURL];
    
    BOOL isMe = IMISME(imMessage.clientId);
    self.playButton.locationRight = isMe;
    self.durantionLabel.textAlignment = isMe?NSTextAlignmentRight:NSTextAlignmentLeft;
    
    self.durantionLabel.textColor = isMe?IMCONFIGM.senderAudioDurationTextColor:IMCONFIGM.recevierAudioDurationTextColor;
    
    [self setNeedsLayout];
}


#pragma mark - layout
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat height = 40.0f;
    CGFloat width = [self.playButton preferredWidth]+IMCONFIGM.messageBackgroundArrowWidth;
    
#define kDurantionLabelMargin 6.0f
#define kUnreadSignWidth 14.0f
    
    CGFloat labelWidth = [self.durantionLabel.text singleWidthWithMaxWidth:self.backgroundImageView.frameX-kDurantionLabelMargin withFont:self.durantionLabel.font];
    
    //左和右
    if (IMISME(self.imMessage.clientId)) {
        self.backgroundImageView.frame = CGRectMake(self.avatarImageView.frameX - kElementXSpacing/2 - width, self.avatarImageView.frameY, width, height);
        
        self.playButton.frame = self.backgroundImageView.bounds;
        
        self.durantionLabel.frame = CGRectMake(self.backgroundImageView.frameX-kDurantionLabelMargin-labelWidth, self.backgroundImageView.frameY+kUnreadSignWidth, labelWidth, self.backgroundImageView.frameHeight-kUnreadSignWidth);
        
        self.unreadSignImageView.frame = CGRectMake(self.durantionLabel.frameX, self.backgroundImageView.frameY, kUnreadSignWidth,kUnreadSignWidth);
    }else{
        self.backgroundImageView.frame = CGRectMake(self.avatarImageView.frameRight + kElementXSpacing/2, self.avatarImageView.frameY, width, height);
        
        self.playButton.frame = self.backgroundImageView.bounds;
        
        self.durantionLabel.frame = CGRectMake(self.backgroundImageView.frameRight+kDurantionLabelMargin, self.backgroundImageView.frameY+kUnreadSignWidth, labelWidth, self.backgroundImageView.frameHeight-kUnreadSignWidth);
        
        self.unreadSignImageView.frame = CGRectMake(self.durantionLabel.frameRight-kUnreadSignWidth, self.backgroundImageView.frameY, kUnreadSignWidth, kUnreadSignWidth);
    }
    
    
    [self layoutEnd];
}

- (void)layoutEnd
{
    [super layoutEnd];
    
    if (IMISME(self.imMessage.clientId)) {
        self.indicator.frameX = self.durantionLabel.frameX-10.0f-kIndicatorSideLength;
        self.sendFailImageView.frameX = self.durantionLabel.frameX-10.0f-kSendFailedSignSideLength;
    }
}


@end
